﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatabaseSimulation
{
    public class TransactionGenerator
    {
        private DatabaseOperationGenerator DatabaseOperationGenerator { get; set; }
        private Random Random { get; set; }
        private GaussianRandom GaussianRandom { get; set; }

        public int NumberOfOperationsMean { get; private set; }
        public int NumberOfOperationsStdDev { get; private set; }

        public TransactionGenerator(DatabaseOperationGenerator databaseOperationGenerator, Random random, GaussianRandom gaussianRandom, int numberOfOperationsMean, int numberOfOperationsStdDev)
        {
            this.DatabaseOperationGenerator = databaseOperationGenerator;
            this.Random = random;
            this.GaussianRandom = gaussianRandom;
            this.NumberOfOperationsMean = numberOfOperationsMean;
            this.NumberOfOperationsStdDev = numberOfOperationsStdDev;
        }

        private int GetRandomNumberOfOperations()
        {
            return Math.Max(1, GaussianRandom.NextGaussianInt(NumberOfOperationsMean, NumberOfOperationsStdDev));
        }

        public Transaction GenerateTransaction(int numberOfObjects)
        {
            Transaction transaction = new Transaction();
            List<DatabaseOperation> operations = new List<DatabaseOperation>();
            for (int i = 0; i < this.GetRandomNumberOfOperations(); i++)
            {
                operations.Add(this.DatabaseOperationGenerator.GenerateDatabaseOperation(transaction, numberOfObjects));
            }
            transaction.Operations = operations;
            return transaction;
        }
    }
}
